package pers.vicxu.mvcrestplus.formatter;

import org.springframework.format.Formatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Create by Vic Xu on 7/25/2018
 *
 * @author Vic Xu
 */
public class DateFormatter implements Formatter<Date> {

    private static final ThreadLocal<DateFormat> SIMPLE_DATE_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        return SIMPLE_DATE_FORMAT.get().parse(text);
    }

    @Override
    public String print(Date object, Locale locale) {
        return SIMPLE_DATE_FORMAT.get().format(object);
    }
}
